package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐信息
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("套餐信息添加成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> selectAll(int page, int pageSize, String name) {
        //获取分页工具类
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        //进行条件分页查询
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo, queryWrapper);
        //records存放的是所有的数据
        List<Setmeal> records = pageInfo.getRecords();
        ArrayList<SetmealDto> arrayList = new ArrayList<>();
        //遍历records，来获取里面的值，并交给ArrayList
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(record, setmealDto);
            //record存放的数据是Setmeal对象，可以通过categoryid去找到name，在赋值给dtoPage的categoryName属性
            Category category = categoryService.getById(record.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            //将元素添加到集合里
            arrayList.add(setmealDto);
        }
        //将结果添加到新返回的page数据中
        dtoPage.setRecords(arrayList);
        return R.success(dtoPage);
    }

    /**
     * 修改菜品状态;支持批量停售和批量启售
     * @param ids    待修改套餐的id集合
     * @param status 各个套餐的状态，0或者1,因为请求路径带有status，故直接启用，停用合为一个方法执行
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@RequestParam List<Long> ids, @PathVariable int status) {
        log.info("ids:{},status:{}", ids, status);
        for (Long id : ids) {
            //通过id查找每个dish数据，在进行设置
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("修改套餐状态成功");
    }

    /**
     * 删除套餐，删除时将套餐-菜品关联表中数据一同删除
     * 清理缓存所有套餐数据
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteSetmeal(@RequestParam List<Long> ids) {//也是url传递
        log.info("ids:{}", ids);
        boolean flag = setmealService.deleteSetmeal(ids);
        if (flag) {
            return R.success("删除套餐成功");
        }
        return R.error("删除套餐失败");
    }

    /**
     * 根据条件查询套餐信息,用于返回前端 展示点餐的套餐信息
     * @param setmeal
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {//前端使用url传递数据，直接用实体类接收
        //条件查询
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        //排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }
}